﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using WindowsForms.AOnline.Model.BusinessObjects.Dictionary;
using WindowsForms.AOnline.Presenter.Dictionary.Department;
using WindowsForms.AOnline.View.Dictionary;
using WindowsForms.AOnline.WindowsForm.FormBase;
using WindowsForms.AOnline.WindowsForm.FormBase.PropertyGrid;
using WindowsForms.AOnline.WindowsForm.Resources;

namespace WindowsForms.AOnline.WindowsForm.FormDictionary
{
    public partial class FrmXtraDepartmentDetail : FrmXtraBaseTreeDetail, IDepartmentView, IDepartmentsView
	{
        private readonly DepartmentPresenter _departmentPresenter;
        private readonly DepartmentsPresenter _departmentsPresenter;

        public FrmXtraDepartmentDetail()
		{
            InitializeComponent();
            _departmentPresenter = new DepartmentPresenter(this);
            _departmentsPresenter = new DepartmentsPresenter(this);
		}

        #region IDepartmentView Members
        public long DepartmentId { get; set; }

        public int? ParentId
        {
            get
            {
                if (grdLockUpParentId.EditValue == null) return null;
                return (int?)grdLockUpParentId.GetColumnValue(KeyFieldName);
            }
            set
            {
                grdLockUpParentId.EditValue = value;
            }
        }

        public string Code
        {
            get { return txtDepartmentCode.Text; }
            set { txtDepartmentCode.Text = value; }
        }

        public long UnitId { get; set; }
        public string Address
        {
            get { return ""; }
            set
            {
                 ;
            }
        }

        public string PhoneNumber
        {
            get { return ""; }
            set
            {
                ;
            }
        }

        public string Fax
        {
            get { return ""; }
            set
            {
                value = value; 
            }
        }

        public string Description
        {
            get { return memoDescription.Text; }
            set { memoDescription.Text = value; }
        }

        public bool IsActive
        {
            get { return chkIsActive.Checked; }
            set { chkIsActive.Checked = value; }
        }


        public string DepartmentName
        {
            get { return txtDepartmentName.Text; }
            set { txtDepartmentName.Text = value; }
        }

        #endregion

        #region IDepartmentsView Members

        public IList<Model.BusinessObjects.Dictionary.DepartmentModel> Departments
        {
            set
            {
                grdLockUpParentId.Properties.DataSource = value;
                grdLockUpParentId.Properties.PopulateColumns();
                //var treeColumnsCollection = new List<XtraColumn> { 
                //                                new XtraColumn { ColumnName = "DepartmentId", ColumnVisible = false },
                //                                new XtraColumn { ColumnName = "ParentId", ColumnVisible = false },
                //                                new XtraColumn { ColumnName = "DepartmentCode", ColumnCaption = "Mã phòng ban", ColumnPosition = 1, ColumnVisible = true },
                //                                new XtraColumn { ColumnName = "DepartmentName", ColumnCaption = "Tên phòng ban", ColumnPosition = 2, ColumnVisible = true },
                //                                new XtraColumn { ColumnName = "Description", ColumnVisible = false },
                //                                new XtraColumn { ColumnName = "IsActive", ColumnVisible = false }
                //                            };

                //foreach (var column in treeColumnsCollection)
                //{
                //    if (column.ColumnVisible)
                //    {
                //        grdLockUpParentId.Properties.Columns[column.ColumnName].Caption = column.ColumnCaption;
                //        grdLockUpParentId.Properties.SortColumnIndex = column.ColumnPosition;
                //    }
                //    else grdLockUpParentId.Properties.Columns[column.ColumnName].Visible = false;
                //}
                grdLockUpParentId.Properties.DisplayMember = "Name";
                grdLockUpParentId.Properties.ValueMember = KeyFieldName;
            }
        }
        #endregion

        /// <summary>
        /// Initializes the data.
        /// </summary>
        protected override void InitData()
        {
            //_departmentsPresenter.Display();
            //if (KeyValue != null) { _departmentPresenter.Display(KeyValue); }
            //else
            //{
            //    if (CurrentNode != null)
            //    {
            //        txtDepartmentCode.Text = ((DepartmentModel)CurrentNode).Code;
            //        txtDepartmentName.Text = @"Phòng ban mới";
            //        grdLockUpParentId.Text = ((DepartmentModel)CurrentNode).Name;
            //    }
            //}

            if (KeyValue != null)
            {
                _departmentPresenter.Display(KeyValue);
                _departmentsPresenter.DisplayForComboTree(int.Parse(KeyValue));
                grdLockUpParentId.Enabled = false;
            }
            else
            {
                //   _accountsPresenter.Display(cu);
                _departmentsPresenter.GetAll();
            }
        }

        /// <summary>
        /// Focuses the control.
        /// </summary>
        protected override void InitControls()
        {
            grdLockUpParentId.Properties.Enabled = (ActionMode != ActionModeEnum.Edit) || !HasChildren;
        }

        /// <summary>
        /// Valids the data.
        /// </summary>
        /// <returns></returns>
        protected override bool ValidData()
        {
            if (string.IsNullOrEmpty(Code))
            {
                XtraMessageBox.Show(ResourceHelper.GetResourceValueByName("ResDepartmentCode"),
                                ResourceHelper.GetResourceValueByName("ResDetailContent"), MessageBoxButtons.OK, MessageBoxIcon.Error);
                txtDepartmentCode.Focus();
                return false;
            }
            if (string.IsNullOrEmpty(DepartmentName))
            {
                XtraMessageBox.Show(ResourceHelper.GetResourceValueByName("ResDepartmentName"),
                                ResourceHelper.GetResourceValueByName("ResDetailContent"), MessageBoxButtons.OK, MessageBoxIcon.Error);
                txtDepartmentName.Focus();
                return false;
            }
            if (ActionMode == ActionModeEnum.Edit)
            {
                if (DepartmentName == grdLockUpParentId.Text)
                {
                    XtraMessageBox.Show(ResourceHelper.GetResourceValueByName("ResDepartmentNameSameAsParentError"),
                                    ResourceHelper.GetResourceValueByName("ResDetailContent"), MessageBoxButtons.OK, MessageBoxIcon.Error);
                    grdLockUpParentId.Focus();
                    return false;
                }
            }
            return true;
        }

        /// <summary>
        /// Saves the data.
        /// </summary>
        /// <returns></returns>
        protected override string SaveData()
        {
            return _departmentPresenter.Save();
        }

        private void grdLockUpParentId_KeyDown(object sender, KeyEventArgs e)
        {
            if (grdLockUpParentId.SelectionLength == grdLockUpParentId.Text.Length && (e.KeyData == Keys.Back || e.KeyData == Keys.Delete))
            {
                grdLockUpParentId.EditValue = null;
                e.Handled = true;
            }
        }
    }
}